package org.standard.suggest.action;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.standard.base.model.BaseRole;
import org.standard.base.model.BaseUser;
import org.standard.commons.struts2.BaseAction;
import org.standard.commons.utils.Constants;
import org.standard.commons.utils.FlipPageInfo;
import org.standard.commons.utils.Utils;
import org.standard.suggest.model.Suggest;
import org.standard.tianjieci.model.Tianjieci;

public class SuggestAction extends BaseAction {

	private Suggest suggest;
	private FlipPageInfo flipPageInfo = new FlipPageInfo();
	private FlipPageInfo flipPageInfo1 = new FlipPageInfo();
	private FlipPageInfo flipPageInfo2 = new FlipPageInfo();
	private FlipPageInfo flipPageInfo3 = new FlipPageInfo();
	private FlipPageInfo flipPageInfo4 = new FlipPageInfo();
	private FlipPageInfo flipPageInfo5 = new FlipPageInfo();
	private List scoreSumList = new ArrayList();
	
	private File[] annex;
	private String[] annexFileName;
	private String[] annexContentType;
	
	private String s_theme;
	private Date startDate;
	private Date endDate;
	private List annexList;
	private BaseUser user;
	private String s_userName;
	private String s_classify;
	private String type;
	private String isRead;
	
	
	public String index() {
		
		BaseRole role = (BaseRole) session.get(Constants.SESSION_ROLE);
		BaseUser user = (BaseUser) session.get(Constants.SESSION_USER);
		if (role.getRoleId().equals(Constants.ROLE_COMMITTEE)) {
			List list = service.findPOJOList("from Tianjieci");
			if (list != null && !list.isEmpty()) {
				Tianjieci tianjieci = (Tianjieci) list.get(0);
				suggest = new Suggest();
				suggest.setNo(tianjieci.getNo());
				suggest.setUserName(user.getUserName());
			}
			return CREATE;
		} else {
			return list();
		}
	}
	
	public String create() {
		
		BaseUser user = (BaseUser) session.get(Constants.SESSION_USER);
		suggest.setUserId(user.getUserId());
		suggest.setUserName(user.getUserName());
		service.createPOJO(suggest);
		return search();
	}

	public String list() {
		return search();
	}
	public String search() {
		
		setSearchDate();
		BaseRole role = (BaseRole) session.get(Constants.SESSION_ROLE);
		BaseUser user = (BaseUser) session.get(Constants.SESSION_USER);
		StringBuffer sql = new StringBuffer();
		if (role.getRoleId().equals(Constants.ROLE_COMMITTEE)) {
			sql.append("select suggest.id, suggest.theme, suggest.date from Suggest suggest where 1=1");
			sql.append(" and suggest.user_Id = '" + user.getUserId() + "'");
		} else {
			sql.append("select suggest.id, suggest.theme, suggest.date, " +
					" suggest.status, suggest.no, " +
					" if(suggest.user_id_read like '%" + user.getUserId() + "%'" + ",'Y','N'), " +
					" if(suggest.role_id_read like '%0%'" + ",'Y','N') " +
					" from Suggest suggest where 1=1");
		}
		if (!Utils.isStringNull(s_theme)) {
			sql.append(" and suggest.theme like '%" + s_theme + "%'");
		}
		if (!Utils.isStringNull(s_userName)) {
			sql.append(" and suggest.user_Name like '%" + s_userName + "%'");
		}
		if (!Utils.isStringNull(s_classify)) {
			sql.append(" and suggest.classify = '" + s_userName + "'");
		}
		if (null != startDate) {
			sql.append(" and suggest.date >= '"+Utils.formatDateString(startDate)+"'");
		}
		if (null != endDate) {
			sql.append(" and suggest.date <= '"+Utils.formatDateString(endDate)+"'");
		}
		sql.append(" order by suggest.date desc");
		flipPageInfo = service.findWithPagingBySql(sql.toString(), flipPageInfo);
		
		if (role.getRoleId().equals(Constants.ROLE_COMMITTEE)) {
			return LIST;
		} else {
			return "list_manager";
		}
		
	}
	private void setSearchDate() {
		
		if (startDate == null && endDate == null) {
			String currentYear = Utils.getCurrentDateString("yyyy");
			int currentMonth = Integer.parseInt(Utils.getCurrentDateString("MM"));
			if (1 <= currentMonth && currentMonth <= 3) {
				setStartDate(Utils.formatStringDate(currentYear + "-01-01"));
				setEndDate(Utils.formatStringDate(currentYear + "-03-31"));
			}
			if (4 <= currentMonth && currentMonth <= 6) {
				setStartDate(Utils.formatStringDate(currentYear + "-04-01"));
				setEndDate(Utils.formatStringDate(currentYear + "-06-30"));
			}
			if (7 <= currentMonth && currentMonth <= 9) {
				setStartDate(Utils.formatStringDate(currentYear + "-07-01"));
				setEndDate(Utils.formatStringDate(currentYear + "-09-30"));
			}
			if (10 <= currentMonth && currentMonth <= 12) {
				setStartDate(Utils.formatStringDate(currentYear + "-10-01"));
				setEndDate(Utils.formatStringDate(currentYear + "-12-31"));
			}
		}
	}

	public String delete() {
		
		try {
			service.deletePOJOCascadeAnnex(suggest, suggest.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return search();
	}
	public String modifyPre() {
		
//		if (suggest != null) {
//			suggest = (Suggest) service.findPOJOById(Suggest.class, suggest.getId());
//			annexList = service.findPOJOList("from Annex where tableName = '"+Suggest.class.getSimpleName()+"' and tableId = '"+suggest.getId()+"'");
//			user = (BaseUser) service.findPOJOById(BaseUser.class, suggest.getUserId());
//		}
//		BaseRole role = (BaseRole) session.get(Constants.SESSION_ROLE);
//		if (role.getRoleId().equals(Constants.ROLE_STAFF)) {
//			if (Utils.isStringNull(suggest.getNo())) {
//				List list = service.findPOJOList("from Tianjieci");
//				if (list != null && !list.isEmpty()) {
//					Tianjieci tianjieci = (Tianjieci) list.get(0);
//					suggest.setNo(tianjieci.getNo());
//				}
//			}
//		}
		suggest = (Suggest) service.findPOJOById(Suggest.class, suggest.getId());
		if ("N".equals(isRead)) {
			BaseRole role = (BaseRole) session.get(Constants.SESSION_ROLE);
			BaseUser user = (BaseUser) session.get(Constants.SESSION_USER);
			String userIdRead = "";
			if (!Utils.isStringNull(suggest.getUserIdRead())) {
				userIdRead = suggest.getUserIdRead();
			}
			suggest.setUserIdRead(userIdRead + user.getUserId() + ",");
			
			String roleIdRead = "";
			if (!Utils.isStringNull(suggest.getRoleIdRead())) {
				roleIdRead = suggest.getRoleIdRead();
			}
			suggest.setRoleIdRead(roleIdRead + role.getRoleId() + ",");
			service.modifyPOJO(suggest);
		}
		
		return MODIFY;
	}
	public String modify() {
		
//		BaseRole role = (BaseRole) session.get(Constants.SESSION_ROLE);
		//工作人员
//		if (role.getRoleId().equals(Constants.ROLE_STAFF)) {
//			if (suggest.getStatus().equals(Constants.SUGGEST_NONE)) {
//				suggest.setStatus(Constants.SUGGEST_STAFF_SUBMIT);
//			}
//			BaseUser user = (BaseUser) session.get(Constants.SESSION_USER);
//			suggest.setPfrName(user.getUserName());
//		}
//		setScoreTotal();
//		try {
//			service.modifyPOJOAndAnnex(suggest, request, annex, annexFileName, annexContentType, suggest.getId());
//		} catch (FileUploadException e) {
//			setERROR(e.getMessage());
//			e.printStackTrace();
//			return MODIFY;
//		} catch (SQLException e) {
//			setERROR(e.getMessage());
//			e.printStackTrace();
//			return MODIFY;
//		}
		service.modifyPOJO(suggest);
		return search();
	}
	public String detail() {
		modifyPre();
		return DETAIL;
	}
	/**
	 * 省政协得分：省政协采用+全国政协采用；
		市政协得分：市政协采用+吕梁日报采用；
		本季度分值：省政协得分+市政协得分；
		全年省政协：一年内省政协得分值；
		全年市政协：一年内市政协得分值；
		总分值：全年省政协+全年市政协；
	 */
	public String score() {
		int num = 100;
		//总分值
		StringBuffer sql = new StringBuffer("select dept.dept_Name, suggest.score_Total")
		.append(" from Base_Dept dept")
		.append(" left join Base_User user")
		.append(" on dept.dept_Id = user.dept_Id ")
		.append(" left join Suggest suggest")
		.append(" on suggest.user_Id = user.user_Id")
		.append(" and DATE_FORMAT(suggest.date, '%Y')= YEAR(NOW()) ")
		.append(" group by dept.dept_Name");
		flipPageInfo.setSize(num);
		flipPageInfo = service.findWithPagingBySql(sql.toString(), flipPageInfo);
		Integer temp = 0;
		for (int i = 0; i < flipPageInfo.getData().size(); i++) {
			Object obj[] = (Object[]) flipPageInfo.getData().get(i);
			temp += Utils.formatObjToInteger(obj[1]);
		}
		scoreSumList.add(temp);
		setSearchDate();
		//省政协得分
		StringBuffer sql1 = new StringBuffer("select suggest.score_Total")
		.append(" from Base_Dept dept")
		.append(" left join Base_User user")
		.append(" on dept.dept_Id = user.dept_Id ")
		.append(" left join Suggest suggest")
		.append(" on suggest.user_Id = user.user_Id")
		.append(" and suggest.date >= '"+Utils.formatDateString(startDate)+"'")
		.append(" and suggest.date <= '"+Utils.formatDateString(endDate)+"'")
		.append(" and (suggest.dept_Employ = '3' or suggest.dept_Employ = '4')")
		.append(" group by dept.dept_Name");
		flipPageInfo1.setSize(num);
		flipPageInfo1 = service.findWithPagingBySql(sql1.toString(), flipPageInfo1);
		calScoreSumList(flipPageInfo1);
		//市政协得分
		StringBuffer sql2 = new StringBuffer("select suggest.score_Total")
		.append(" from Base_Dept dept")
		.append(" left join Base_User user")
		.append(" on dept.dept_Id = user.dept_Id ")
		.append(" left join Suggest suggest")
		.append(" on suggest.user_Id = user.user_Id")
		.append(" and suggest.date >= '"+Utils.formatDateString(startDate)+"'")
		.append(" and suggest.date <= '"+Utils.formatDateString(endDate)+"'")
		.append(" and (suggest.dept_Employ = '1' or suggest.dept_Employ = '2')")
		.append(" group by dept.dept_Name");
		flipPageInfo2.setSize(num);
		flipPageInfo2 = service.findWithPagingBySql(sql2.toString(), flipPageInfo2);
		calScoreSumList(flipPageInfo2);
		//本季度分值：省政协得分+市政协得分
		StringBuffer sql3 = new StringBuffer("select suggest.score_Total")
		.append(" from Base_Dept dept")
		.append(" left join Base_User user")
		.append(" on dept.dept_Id = user.dept_Id ")
		.append(" left join Suggest suggest")
		.append(" on suggest.user_Id = user.user_Id")
		.append(" and suggest.date >= '"+Utils.formatDateString(startDate)+"'")
		.append(" and suggest.date <= '"+Utils.formatDateString(endDate)+"'")
		.append(" group by dept.dept_Name");
		flipPageInfo3.setSize(num);
		flipPageInfo3 = service.findWithPagingBySql(sql3.toString(), flipPageInfo3);
		calScoreSumList(flipPageInfo3);
		//全年省政协：一年内省政协得分值；
		StringBuffer sql4 = new StringBuffer("select suggest.score_Total")
		.append(" from Base_Dept dept")
		.append(" left join Base_User user")
		.append(" on dept.dept_Id = user.dept_Id ")
		.append(" left join Suggest suggest")
		.append(" on suggest.user_Id = user.user_Id")
		.append(" and DATE_FORMAT(suggest.date, '%Y')= YEAR(NOW()) ")
		.append(" and (suggest.dept_Employ = '3' or suggest.dept_Employ = '4')")
		.append(" group by dept.dept_Name");
		flipPageInfo4.setSize(num);
		flipPageInfo4 = service.findWithPagingBySql(sql4.toString(), flipPageInfo4);
		calScoreSumList(flipPageInfo4);
		//全年市政协：一年内市政协得分值；
		StringBuffer sql5 = new StringBuffer("select suggest.score_Total")
		.append(" from Base_Dept dept")
		.append(" left join Base_User user")
		.append(" on dept.dept_Id = user.dept_Id ")
		.append(" left join Suggest suggest")
		.append(" on suggest.user_Id = user.user_Id")
		.append(" and DATE_FORMAT(suggest.date, '%Y')= YEAR(NOW()) ")
		.append(" and (suggest.dept_Employ = '1' or suggest.dept_Employ = '2')")
		.append(" group by dept.dept_Name");
		flipPageInfo5.setSize(num);
		flipPageInfo5 = service.findWithPagingBySql(sql5.toString(), flipPageInfo5);
		calScoreSumList(flipPageInfo5);
		return "score";
	}
	public String group() {
		
		int num = 100;
		if ("1".equals(type)) {
			StringBuffer sql = new StringBuffer("select dept.dept_Name, count(*)")
			.append(" from Base_Dept dept, ")
			.append(" Base_User user, Suggest suggest")
			.append(" where dept.dept_Id = user.dept_Id ")
			.append(" and suggest.user_Id = user.user_Id")
			.append(" group by dept.dept_Name");
			flipPageInfo.setSize(num);
			flipPageInfo = service.findWithPagingBySql(sql.toString(), flipPageInfo);
			calGroupScoreSumList(flipPageInfo);
		} else if ("2".equals(type)) {
			StringBuffer sql = new StringBuffer("select dept.dept_Name, count(user.user_Id)")
			.append(" from Base_Dept dept, ")
			.append(" Base_User user, Suggest suggest")
			.append(" where dept.dept_Id = user.dept_Id ")
			.append(" and suggest.user_Id = user.user_Id")
			.append(" group by dept.dept_Name");
			flipPageInfo.setSize(num);
			flipPageInfo = service.findWithPagingBySql(sql.toString(), flipPageInfo);
			calGroupScoreSumList(flipPageInfo);
		} else if ("3".equals(type)) {
			StringBuffer sql = new StringBuffer("select dept.dept_Name, count(user.jiebie)")
			.append(" from Base_Dept dept, ")
			.append(" Base_User user, Suggest suggest")
			.append(" where dept.dept_Id = user.dept_Id ")
			.append(" and suggest.user_Id = user.user_Id")
			.append(" group by dept.dept_Name");
			flipPageInfo.setSize(num);
			flipPageInfo = service.findWithPagingBySql(sql.toString(), flipPageInfo);
			calGroupScoreSumList(flipPageInfo);
		}
		return "score";
	}
	private void calGroupScoreSumList(FlipPageInfo flipPageInfo) {
		Integer temp = 0;
		for (int i = 0; i < flipPageInfo.getData().size(); i++) {
			Object obj[] = (Object []) flipPageInfo.getData().get(i);
			temp += Utils.formatObjToInteger(obj[1]);
		}
		scoreSumList.add(temp);
	}
	
	private void calScoreSumList(FlipPageInfo flipPageInfo) {
		Integer temp = 0;
		for (int i = 0; i < flipPageInfo.getData().size(); i++) {
			Object obj = flipPageInfo.getData().get(i);
			temp += Utils.formatObjToInteger(obj);
		}
		scoreSumList.add(temp);
	}

	public Suggest getSuggest() {
		return suggest;
	}

	public void setSuggest(Suggest suggest) {
		this.suggest = suggest;
	}

	public FlipPageInfo getFlipPageInfo() {
		return flipPageInfo;
	}

	public void setFlipPageInfo(FlipPageInfo flipPageInfo) {
		this.flipPageInfo = flipPageInfo;
	}

	public File[] getAnnex() {
		return annex;
	}

	public void setAnnex(File[] annex) {
		this.annex = annex;
	}

	public String[] getAnnexFileName() {
		return annexFileName;
	}

	public void setAnnexFileName(String[] annexFileName) {
		this.annexFileName = annexFileName;
	}

	public String[] getAnnexContentType() {
		return annexContentType;
	}

	public void setAnnexContentType(String[] annexContentType) {
		this.annexContentType = annexContentType;
	}

	public String getS_theme() {
		return s_theme;
	}

	public void setS_theme(String s_theme) {
		this.s_theme = s_theme;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public List getAnnexList() {
		return annexList;
	}

	public void setAnnexList(List annexList) {
		this.annexList = annexList;
	}

	public BaseUser getUser() {
		return user;
	}

	public void setUser(BaseUser user) {
		this.user = user;
	}

	public FlipPageInfo getFlipPageInfo1() {
		return flipPageInfo1;
	}

	public void setFlipPageInfo1(FlipPageInfo flipPageInfo1) {
		this.flipPageInfo1 = flipPageInfo1;
	}

	public FlipPageInfo getFlipPageInfo2() {
		return flipPageInfo2;
	}

	public void setFlipPageInfo2(FlipPageInfo flipPageInfo2) {
		this.flipPageInfo2 = flipPageInfo2;
	}

	public FlipPageInfo getFlipPageInfo3() {
		return flipPageInfo3;
	}

	public void setFlipPageInfo3(FlipPageInfo flipPageInfo3) {
		this.flipPageInfo3 = flipPageInfo3;
	}

	public FlipPageInfo getFlipPageInfo4() {
		return flipPageInfo4;
	}

	public void setFlipPageInfo4(FlipPageInfo flipPageInfo4) {
		this.flipPageInfo4 = flipPageInfo4;
	}

	public FlipPageInfo getFlipPageInfo5() {
		return flipPageInfo5;
	}

	public void setFlipPageInfo5(FlipPageInfo flipPageInfo5) {
		this.flipPageInfo5 = flipPageInfo5;
	}

	public String getS_userName() {
		return s_userName;
	}

	public void setS_userName(String name) {
		s_userName = name;
	}

	public List getScoreSumList() {
		return scoreSumList;
	}

	public void setScoreSumList(List scoreSumList) {
		this.scoreSumList = scoreSumList;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getS_classify() {
		return s_classify;
	}

	public void setS_classify(String s_classify) {
		this.s_classify = s_classify;
	}

	public String getIsRead() {
		return isRead;
	}

	public void setIsRead(String isRead) {
		this.isRead = isRead;
	}
	
	
	
}
